package org.chnxi.study.serialize.gson;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import java.util.ArrayList;

public class GsonTest {

    public static void main(String[] args) {
        HeadTeacher ht = new HeadTeacher();
        EnglishTeacher et = new EnglishTeacher();
        ArrayList<Teacher> teachers = new ArrayList<>();
        teachers.add(ht);
        teachers.add(et);

        Student s = new Student();
        s.setName("张三");
        s.setTeachers(teachers);

        ArrayList<Student> studentes = new ArrayList<Student>();
        studentes.add(s);
        et.setStudentes(studentes);

        EnglishBook eb = new EnglishBook();
        ChineseBook cb = new ChineseBook();
        ArrayList<Book> books = new ArrayList<>();
        books.add(eb);
        books.add(cb);
        s.setBooks(books);

        Gson gson = new Gson();
        String str = gson.toJson(s);
        System.out.println(str);

        GsonBuilder builder = new GsonBuilder();
        builder.registerTypeAdapter(Teacher.class, new GsonTypeAdapter<Teacher>());
        builder.registerTypeAdapter(Book.class , new GsonTypeAdapter<Book>());
        Gson mygson = builder.create();
        String myjson = mygson.toJson(s);
        System.out.println(myjson);

        Student s2 = mygson.fromJson(myjson , Student.class);
        System.out.println(s2);

    }

}
